# -----------------------------------------------------------------------------------------------------------
# Copyright (c) 2025 Huawei Technologies Co., Ltd.
# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
# CANN Open Software License Agreement Version 2.0 (the "License").
# Please refer to the License for details. You may not use this file except in compliance with the License.
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
# See LICENSE in the root of the software repository for the full text of the License.
# -----------------------------------------------------------------------------------------------------------
add_custom_target(hccl-0.1.0-py3-none-any.whl ALL
    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/dist/hccl-0.1.0-py3-none-any.whl
)

# 读取sys_version.conf中版本号并打包到whl包中的hccl目录下
file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/../version.info  SYS_VERSION)

string(FIND "${SYS_VERSION}" "=" EQUALS_POS)
math(EXPR VERSION_START "${EQUALS_POS} + 1")

string(FIND "${SYS_VERSION}" ";" SEMICOLON_POS)
if(${SEMICOLON_POS} EQUAL -1)
    set(SEMICOLON_POS ${SYS_VERSION_LENGTH})
endif()

# 计算版本号的长度
math(EXPR VERSION_LENGTH "${SEMICOLON_POS} - ${VERSION_START}")

# 提取版本号
string(SUBSTRING "${SYS_VERSION}" ${VERSION_START} ${VERSION_LENGTH} VERSION_NUMBER)

message(STATUS "Extracted version number: ${VERSION_NUMBER}")

set(SYS_VERSION ${VERSION_NUMBER})

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/sys_version.py.in ${CMAKE_CURRENT_BINARY_DIR}/hccl/sys_version.py @ONLY)

add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/dist/hccl-0.1.0-py3-none-any.whl
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/setup.py ${CMAKE_CURRENT_BINARY_DIR}
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/hccl ${CMAKE_CURRENT_BINARY_DIR}/hccl
	
    COMMAND ${HI_PYTHON} setup.py bdist_wheel
)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/dist/hccl-0.1.0-py3-none-any.whl
    DESTINATION ${INSTALL_LIBRARY_DIR} OPTIONAL
)
